package com.wyd.boot.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Kafka {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    // 主题 中文报错
//    private static final String topic = "主题1";
    private static final String topic = "topic_1";

    @GetMapping("/send")
    public String sendMessage(String message) {
        kafkaTemplate.send(topic, message);

        return "ok:" + message;
    }

    @KafkaListener(topics = topic)
    public void receiveMessage(String message) {
        // 处理接收到的消息
        System.out.println("监听到消息：" + message);
    }
}
